// page/component/details/details.js

const app = getApp()
Page({
    data:{
        windowHeight: 654,
        maxtime: "",
        isHiddenLoading: true,
        isHiddenToast: true,
        dataList: {},
        countDownDay: 0,
        countDownHour: 0,
        countDownMinute: 0,
        countDownSecond: 0,
        cart: [
          {
            foodId: 0,
            name:'',
            price:0,
            count:0,
            image:''
          }

        ],
      goods:  {
        endTime1:'2021/12/30'
      },
      goods1: {
        id: 1,
        image: 'https://z3.ax1x.com/2021/06/14/2786G6.jpg',
        title: '蒙都牛肉干5kg',
        price: 99,
        stock: 233,
        detail: '这里是详情。',
        parameter: '125g/个',
        service: '不支持退货',
        time_remaining:23,//剩余时间，记录小时
        day:0
      },
      comment_list:[],
      comment_list1:[
        {
            id:0,
            username:'123',
            comment:"商品不错，下次还来回购",
            imageUrl:'/images/banggou.jpg',
            pictures:[
                'https://z3.ax1x.com/2021/06/14/2786G6.jpg',
                'https://z3.ax1x.com/2021/07/05/RhJtQP.jpg',
                'https://z3.ax1x.com/2021/07/05/RhJJzt.jpg'
                
                
            ],
            star: 3.5,//评分
            date:"2021-6-23"
            
        },
        {
          id:1,
          username:'123',
          comment:"商品不错，下次还来回购。哈哈哈哈哈哈哈哈很好很好拍啊",
          imageUrl:'/images/banggou.jpg',
          pictures:[
            'https://z3.ax1x.com/2021/06/14/2786G6.jpg',
                'https://z3.ax1x.com/2021/07/05/RhJtQP.jpg',
                'https://z3.ax1x.com/2021/07/05/RhJJzt.jpg'
        ],
          star: 4.5,//评分
          date:"2021-6-23"
          
      },
      {
        id:2,
        username:'123',
        comment:"商品不错，下次还来回购，不错，刚好需要。",
        imageUrl:'/images/banggou.jpg',
        pictures:[
          'https://z3.ax1x.com/2021/06/14/2786G6.jpg',
          'https://z3.ax1x.com/2021/07/05/RhJtQP.jpg',
          'https://z3.ax1x.com/2021/07/05/RhJJzt.jpg'
      ],
        star: 3,//评分
        date:"2021-6-23"

      },
      {
        id:3,
        username:'123',
        comment:"商品不错，下次还来回购，商品不错，下次还来回购，商品不错，下次还来回购",
        imageUrl:'/images/banggou.jpg',
        pictures:[
          'https://z3.ax1x.com/2021/06/14/2786G6.jpg',
          'https://z3.ax1x.com/2021/07/05/RhJtQP.jpg',
          'https://z3.ax1x.com/2021/07/05/RhJJzt.jpg'
      ],
        star: 3.5,//评分
        date:"2021-6-23"
      },
      {
        id:4,
        username:'123',
        comment:"商品不错，下次还来回购，商品不错，下次还来回购，商品不错，下次还来回购，商品不错，下次还来回购，商品不错，下次还来回购",
        imageUrl:'/images/banggou.jpg',
        pictures:[
          'https://z3.ax1x.com/2021/06/14/2786G6.jpg',
          'https://z3.ax1x.com/2021/07/05/RhJtQP.jpg',
          'https://z3.ax1x.com/2021/07/05/RhJJzt.jpg'
      ],
        star: 2.5,//评分
        date:"2021-6-23"
    },
    {
      id:5,
      username:'123',
      comment:"商品不错，下次还来回购",
      imageUrl:'/images/banggou.jpg',
      pictures:[
        'https://z3.ax1x.com/2021/06/14/2786G6.jpg',
          'https://z3.ax1x.com/2021/07/05/RhJtQP.jpg',
          'https://z3.ax1x.com/2021/07/05/RhJJzt.jpg'
    ],
      star: 5,//评分
      date:"2021-6-23"
  }],
      num: 1,
      totalNum: 0,
      hasCarts: false,
      curIndex: 0,
      show: false,
      scaleCart: false
    },
  
    addCount() {
      let num = this.data.num;
      num++;
      this.setData({
        num : num
      })
    },
  
    // addToCart() {
    //   const self = this;
    //   const num = this.data.num;
    //   let total = this.data.totalNum;
  
    //   self.setData({
    //     show: true
    //   })
    //   setTimeout( function() {
    //     self.setData({
    //       show: false,
    //       scaleCart : true
    //     })
    //     setTimeout( function() {
    //       self.setData({
    //         scaleCart: false,
    //         hasCarts : true,
    //         totalNum: num + total
    //       })
    //     }, 200)
    //   }, 300)
  
    // },

    //下单
    purchase:function(){
      var price = this.data.goods.price*this.data.num;
      var cart = this.data.cart;
      cart[0].foodId = this.data.goods.goodsId;
      cart[0].name= this.data.goods.name;
      cart[0].price = this.data.goods.price;
      cart[0].count = this.data.num;
      cart[0].image = this.data.goods.imagePath;
      console.log(this.data.goods.imagePath);
      //将对象转为string
      var cart1 = JSON.stringify(cart);
      var store = 0
      var storeName = "社区帮购";
      var delPrice = 0;
      var total = price;
      wx.navigateTo({
       url: '../../purchase/purchase?storeId='+store+'&&storeName='+storeName+'&&totalMoney='+total+'&&cart='+cart1+'&&delPrice='+delPrice,
       success: function(res) {
     
       },
       fail: function(res) {
     
       },
       complete: function(res) {
     
       },
      })
     },
  
    bindTap(e) {
      const index = parseInt(e.currentTarget.dataset.index);
      this.setData({
        curIndex: index
      })
    },

    //处理倒计时
    //事件处理函数
 bindViewTap: function() {
    wx.navigateTo( {
     url: '../logs/logs'
    })
   },
   onLoad: function(options) {
     var id = options.id;
     this.getDetail(id);
     this.getComments(id);
     
    this.setData( {
     windowHeight: wx.getStorageSync( 'windowHeight' )
    });
   },
   
    
   getComments: function(id){
    var that = this;
    that.data.token = app.getToken()
    console.log(that.data.token)
    //网络请求获取账单数据
    wx.request({
      url: 'http://localhost:8080/community/helphelp/comments?id='+id,
      method:'GET',
      header:{
        'content-type': 'application/json',
        'Authorization':  that.data.token
      },
      success:function(res){
        // console.log("userInfo" + JSON.stringify(res))
        console.log(res)
        // that.setData({
        //   comment_list: res.data.data
        // });
        var c = res.data.data;
        for(var i=0;i<c.length;i++){
          var pictures = c[i].contentPicture;
          var p = JSON.parse(pictures);
          c[i].contentPicture = p;
          that.setData({
            comment_list: c
          });
        }


      }
   

   })
  },
   getDetail: function(id){
    var that = this;
    that.data.token = app.getToken()
    console.log(that.data.token)
    //网络请求获取账单数据
    wx.request({
      url: 'http://localhost:8080/community/helphelp/detail?id='+id,
      method:'GET',
      header:{
        'content-type': 'application/json',
        'Authorization':  that.data.token
      },
      success:function(res){
        // console.log("userInfo" + JSON.stringify(res))
        console.log(res.data.data.endTime1)
        that.setData({
          goods: res.data.data
        });
        that.getTime(res.data.data.endTime1);

      }
   

   })
  },
         // 页面渲染完成后 调用
   getTime: function (time) {
    var totalSecond = Date.parse(new Date(time)) / 1000 - Date.parse(new Date()) / 1000;
    var interval = setInterval(function () {
     // 秒数
     var second = totalSecond;
     // 天数位
     var day = Math.floor(second / 3600 / 24);
     var dayStr = day.toString();
     if (dayStr.length == 1) dayStr = '0' + dayStr;
     // 小时位
     var hr = Math.floor((second - day * 3600 * 24) / 3600);
     var hrStr = hr.toString();
     if (hrStr.length == 1) hrStr = '0' + hrStr;
     // 分钟位
     var min = Math.floor((second - day * 3600 *24 - hr * 3600) / 60);
     var minStr = min.toString();
     if (minStr.length == 1) minStr = '0' + minStr;
     // 秒位
     var sec = second - day * 3600 * 24 - hr * 3600 - min*60;
     var secStr = sec.toString();
     if (secStr.length == 1) secStr = '0' + secStr;
     this.setData({
      countDownDay: dayStr,
      countDownHour: hrStr,
      countDownMinute: minStr,
      countDownSecond: secStr,
     });
     totalSecond--;
     if (totalSecond < 0) {
      clearInterval(interval);
      wx.showToast({
       title: '活动已结束',
      });
      this.setData({
       countDownDay: '00',
       countDownHour: '00',
       countDownMinute: '00',
       countDownSecond: '00',
      });
     }
    }.bind(this), 1000);
   },


   //cell事件处理函数
   bindCellViewTap: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
     url: '../babyDetail/babyDetail?id=' + id
    });
   },
   //图片点击事件
  imgYu:function(event){
  var src = event.currentTarget.dataset.src;//获取data-src
  var imgList = event.currentTarget.dataset.list;//获取data-list
  //图片预览
  wx.previewImage({
  current: src, // 当前显示图片的http链接
  urls: imgList // 需要预览的图片http链接列表
  })
  }
    
  })